package com.wanda.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wanda.model.JsonGeneralModel;
import com.wanda.model.Sale_ChanceAndRoomModel;
import com.wanda.model.Sale_ChanceModel;
import com.wanda.service.Sale_ChanceService;
import com.wanda.util.DateFormat;



@Controller
@RequestMapping("qt/Sale_Chance")
public class Sale_ChanceController {
	
	@Resource
	private Sale_ChanceService sale_chanceService;
	//private Wd_UserService wd_UserService; 
  
	//添加客户 
	@RequestMapping(value="/addSale_Chance.do")
    public @ResponseBody Map<String,Object> add(Sale_ChanceModel sale_chance){
		System.err.println("tel:"+sale_chance.getTel());
		System.out.println("进入销售机会");
		sale_chance.setState("草稿");
		sale_chance.setAddtime(DateFormat.getNowDate());
		sale_chance.setDelete_state("0");
		sale_chanceService.addSale_Chance(sale_chance);
    	Map<String,Object> map = new HashMap<String,Object>();  
    	System.out.println("添加成功"); 
    	map.put("msg","成功");
    	return map;
    }
	
	
	//修改客户
		@RequestMapping(value="/updateSale_Chance.do")
	    public @ResponseBody Map<String,Object> update(Sale_ChanceModel sale_chance){
			sale_chance.setUpdate_user(" ");
			sale_chance.setState("草稿");
			sale_chance.setDelete_state("0");
			sale_chanceService.updateSale_Chance(sale_chance);
	    	Map<String,Object> map = new HashMap<String,Object>();  
	    	System.out.println("修改成功"); 
	    	
	    	return map;
	    }
		
		//转认购
				@RequestMapping(value="/updateSale_ChanceState.do")
			    public @ResponseBody Map<String,Object> updateSale_ChanceState(Sale_ChanceModel sale_chance){
					sale_chance.setUpdate_user(" ");
					sale_chance.setUpdate_time(DateFormat.getNowDate());
					sale_chance.setDelete_state("0");
					sale_chanceService.updateSale_Chance(sale_chance);
			    	Map<String,Object> map = new HashMap<String,Object>();  
			    	System.out.println("修改成功"); 
			    	
			    	return map;
			    }
		
		
		//删除客户
		@RequestMapping(value="/delete.do")
			 public @ResponseBody Map<String,Object> delete(String sale_chance_id){
				      
			        Sale_ChanceModel sale_chance=new Sale_ChanceModel(); 
			        sale_chance.setSale_chance_id(sale_chance_id);
					String delete_state="1"; 
					sale_chance.setDelete_state(delete_state);
					
					sale_chanceService.deleteSale_Chance(sale_chance);
			    	Map<String,Object> map = new HashMap<String,Object>();  
			    	System.out.println("删除成功"); 
			    	;
			    	return map;
			    }
				
		  //查询所有
				 @RequestMapping(value="/selectAll.do")
				    public @ResponseBody JsonGeneralModel selectAll(String delete_state){
					 System.err.println("查询所有");
						JsonGeneralModel<Sale_ChanceModel> jsonModel = new JsonGeneralModel();
						List<Sale_ChanceModel> sale_chance =null;
						
						try {
						    delete_state="0"; 
						    sale_chance=sale_chanceService.selectAll(delete_state);
							jsonModel.setRows(sale_chance);
						}catch (Exception e) {
							e.printStackTrace();
						}
						return jsonModel;
				    }
    
    //拼接sql查询
	 @RequestMapping(value="/selectBySql.do")
	    public @ResponseBody JsonGeneralModel selectBySql(int page,int rows,String SearchSql){
		 System.out.println("SQL查询");
		 System.err.println("SearchSql:"+SearchSql);
		 JsonGeneralModel<Sale_ChanceModel> jsonModel = new JsonGeneralModel();
			List<Sale_ChanceModel> sale_chance =null;
			int pageIndex=(page-1)*rows;
			int pageSize=0;
			try {
				 String delete_state="0"; 
				 sale_chance=sale_chanceService.selectBySql(pageIndex,rows,SearchSql);
				 pageSize=sale_chanceService.selectAll(delete_state).size() % rows ==0?
						 sale_chanceService.selectAll(delete_state).size() / rows:
							 sale_chanceService.selectAll(delete_state).size() / rows +1;
						 Map<String,Object> data=new HashMap<String,Object>();
						 data.put("rows",sale_chance);
						 jsonModel.setPage(page);
						 jsonModel.setRecords(sale_chanceService.selectAll(delete_state).size());
						 jsonModel.setTotal(pageSize);
						 jsonModel.setRows(sale_chance);
			}catch (Exception e) {
				e.printStackTrace();
			}
			return jsonModel;  
	    }	 
	 
	//拼接sqlALL查询
		 @RequestMapping(value="/selectBySqlAll.do")
		    public @ResponseBody JsonGeneralModel selectBySqlAll( String SearchSql){
			 System.out.println("SQL查询");
			 //System.err.println("SearchSql:"+SearchSql);
			 JsonGeneralModel<Sale_ChanceAndRoomModel> jsonModel = new JsonGeneralModel();
				List<Sale_ChanceAndRoomModel> sale_chance =null;
				
				try {
					    //delete_state="0"; 
					sale_chance=sale_chanceService.selectBySqlAll();
//					for(Sale_ChanceAndRoomModel model:sale_chance)
//					{
//						SearchSql+="and r.room_id='"+model.getRoomModel().getRoom_id()+"'";
//					}
						jsonModel.setRows(sale_chance);
				}catch (Exception e) {
					e.printStackTrace();
				}
				return jsonModel;  
		    }	 

		
}
